package member_coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"bp/internal/util"
	"context"
	"fmt"
)

// GetMemberCouponPtr 获取优惠券
func (s *MemberCouponEntityServiceImp) GetMemberCouponPtr(ctx context.Context, q *sqlc.Queries, memberCouponID int64) (memberCouponPtr *sqlc.MemberCoupon, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)

	memberCoupon, err := q.GetMemberCoupon(ctx, memberCouponID)
	if err != nil {
		return
	}
	memberCouponPtr = &memberCoupon
	return
}

// ListMemberCouponPtrsParam 列出会员优惠券参数
type ListMemberCouponPtrsParam struct {
	MemberCouponStatuses []string
	Keyword              string
}

// ListMemberCouponPtrs 列出会员优惠券
func (s *MemberCouponEntityServiceImp) ListMemberCouponPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListMemberCouponPtrsParam) (total int64, memberCouponPtrs []*sqlc.MemberCoupon, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountMemberCoupons(ctx, sqlc.CountMemberCouponsParams{
		Ts:                          cPtr.PagerPtr.Ts,
		MemberID:                    cPtr.MemberID,
		FilterAllMemberCouponStatus: len(ptr.MemberCouponStatuses) == 0,
		MemberCouponStatuses:        ptr.MemberCouponStatuses,
		IsKeywordEmpty:              util.IsEmpty(ptr.Keyword),
		Keyword:                     fmt.Sprintf("%%%s%%", ptr.Keyword),
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListMemberCoupons(ctx, sqlc.ListMemberCouponsParams{
		Ts:                          cPtr.PagerPtr.Ts,
		MemberID:                    cPtr.MemberID,
		FilterAllMemberCouponStatus: len(ptr.MemberCouponStatuses) == 0,
		MemberCouponStatuses:        ptr.MemberCouponStatuses,
		IsKeywordEmpty:              util.IsEmpty(ptr.Keyword),
		Keyword:                     fmt.Sprintf("%%%s%%", ptr.Keyword),
		Offset:                      cPtr.PagerPtr.Offset,
		Limit:                       cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	// slog.Infof("1 ListMemberCouponPtrs ok:%+v", cPtr.PagerPtr)

	memberCouponPtrs = s.MapMemberCouponEntityToPtr(rows)
	return
}

// ListAvailableMemberCouponsOfUseSceneParam 列出场景可用的会员优惠券参数
type ListAvailableMemberCouponsOfUseSceneParam struct {
	MemberID int64
	BrandID  int64
	UseScene string
}

// ListAvailableMemberCouponsByForDoTrial 列出场景可用的会员优惠券
func (s *MemberCouponEntityServiceImp) ListAvailableMemberCouponsOfUseScene(ctx context.Context, q *sqlc.Queries, ptr *ListAvailableMemberCouponsOfUseSceneParam) (memberCouponPtrs []*sqlc.MemberCoupon, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	memberCoupons, err := q.ListAvailableMemberCouponsByBrandIDIssuerAndUseScene(ctx, sqlc.ListAvailableMemberCouponsByBrandIDIssuerAndUseSceneParams{
		BrandID:  ptr.BrandID,
		MemberID: ptr.MemberID,
		UseScene: ptr.UseScene,
	})
	if err != nil {
		return
	}
	memberCouponPtrs = s.MapMemberCouponEntityToPtr(memberCoupons)
	return
}

type ListMemberCouponsBySnsParam struct {
	MemberID int64
	Sns      []string
}

// ListMemberCouponsBySns 根据优惠券序列号列出会员优惠券
func (s *MemberCouponEntityServiceImp) ListMemberCouponsBySns(ctx context.Context, q *sqlc.Queries, ptr *ListMemberCouponsBySnsParam) (memberCouponPtrs []*sqlc.MemberCoupon, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	memberCoupons, err := q.ListMemberCouponsBySns(ctx, sqlc.ListMemberCouponsBySnsParams{
		MemberID: ptr.MemberID,
		Sns:      ptr.Sns,
	})
	if err != nil {
		return
	}
	memberCouponPtrs = s.MapMemberCouponEntityToPtr(memberCoupons)
	return
}
